<?php require 'head.php' ?>
<?php

$filter_class = "";
if(isset($_GET['class'])){
    $filter_class = $_GET['class'];
}

// 加载页面数据处理
function load_pages() {
  global $state, $index_file, $kw_pages;
  if (isset($_GET['state'])) {  
    if ($_GET['state'] == 'draft') {
      $state = 'draft';
      $index_file = KW_DATA.'pages/index/draft.php';
    }
    else if ($_GET['state'] == 'delete'){
      $state = 'delete';
      $index_file = KW_DATA.'pages/index/delete.php';
    }
    else {
      $state = 'publish';
      $index_file = KW_DATA.'pages/index/publish.php';
    }
  }
  else {
    $state = 'publish';
    $index_file = KW_DATA.'pages/index/publish.php';
  }
  require $index_file;
}

// 删除某个页面
function delete_page($key) {
  global $state, $index_file, $kw_pages;
  $page = $kw_pages[$key];
  $page['prev_state'] = $state;
  unset($kw_pages[$key]);
  file_put_contents($index_file, "<?php\n\$kw_pages=".var_export($kw_pages, true)."\n?>");
  
  if ($state != 'delete') {
    // 存放到回收站里
    $index_file2 = KW_DATA.'pages/index/delete.php';
    require $index_file2;
    $kw_pages[$key] = $page;
    file_put_contents($index_file2, "<?php\n\$kw_pages=".var_export($kw_pages, true)."\n?>");
  } else {
    // 删除文件
    unlink(KW_DATA.'pages/data/'.$key.'.dat');
  }
}
// 还原某个页面
function revert_page($key) {
  global $state, $index_file, $kw_pages;
  $page = $kw_pages[$key];
  $prev_state = $page['prev_state'];
  unset($page['prev_state']);
  unset($kw_pages[$key]);
  file_put_contents($index_file, "<?php\n\$kw_pages=".var_export($kw_pages, true)."\n?>");
  $index_file2 = KW_DATA.'pages/index/'.$prev_state.'.php';
  require $index_file2;
  $kw_pages[$key] = $page;
  ksort($kw_pages);
  file_put_contents($index_file2, "<?php\n\$kw_pages=".var_export($kw_pages, true)."\n?>");
}
// 重载页面数据
load_pages();

if($filter_class != ""){
    
}

$page_keys = array_keys($kw_pages);

$page_count = count($kw_pages);

$last_page = ceil($page_count / 5);

if (isset($_GET['page']))
  $page_num = $_GET['page'];
else
  $page_num = 1;

if ($page_num > 1)
  $prev_page = $page_num - 1;
else
  $prev_page = 1;

if ($page_num < $last_page)
  $next_page = $page_num + 1;
else
  $next_page = $last_page;

if ($page_num < $last_page)
  $next_page = $page_num + 1;
else
  $next_page = $last_page;

if ($page_num < 1)
  $page_num = 1;
else if ($page_num > $last_page)
  $page_num = $last_page;

?>
<script src="js/jquery.js"></script>
<script type="text/javascript">
    function filterByClass(state){
        var filterVal = $("#class").val();
        if(!filterVal){
            return;
        }
        location.href = '?state=' + state + '&class=' + filterVal;
    }
</script>
<?php if (isset($message)) { ?>
<div class="updated"><?php echo $message; ?></div>
<?php } ?>
<div class="admin_page_name">管理页面<a class="link_button" href="pageEdit.php">创建页面</a></div>
<div class="post_mode_link">
<a href="?state=publish" class="link_button <?php if ($state == 'publish') echo 'current'; ?>">已发布</a>
<a href="?state=draft" class="link_button <?php if ($state == 'draft') echo 'current'; ?>">草稿箱</a>
<a href="?state=delete" class="link_button <?php if ($state == 'delete') echo 'current'; ?>">回收站</a>
</div>
<div class="table_list_tool">
  <span>
   <select id="class">
      <option value="">显示所有分类</option>
      <?php foreach ($kw_categories as $class_key => $class_value) { ?>
      <option value="<?php echo $class_key; ?>" <?php if ($filter_class == $class_key) echo ' selected="selected"'; ?>><?php echo $class_value['name']; ?></option>
      <?php } ?>
    </select>
    <input type="submit" value="筛选" onclick="filterByClass('<?php echo $state; ?>');"/>
  </span>
  <span class="pager">
    共 <?php echo $page_count; ?> 项&nbsp;&nbsp;
    <a class="link_button" href="?state=<?php echo $state; ?>">&laquo;</a>
    <a class="link_button" href="?state=<?php echo $state; ?>&page=<?php echo $prev_page; ?>">&lsaquo;</a>
    第 <input type="text" value="<?php echo $page_num; ?>" id="page_input_1"/> / <?php echo $last_page; ?> 页
    <a class="link_button" href="?state=<?php echo $state; ?>&page=<?php echo $next_page; ?>">&rsaquo;</a>
    <a class="link_button" href="?state=<?php echo $state; ?>&page=<?php echo $last_page; ?>">&raquo;</a>
  </span>
</div>
<div class="table_list post_list">
<table colspan="0" rowspan="0" cellpadding="0" cellspacing="0" id="list">
  <thead>
    <tr>
        <td>NO.</td>
        <td>标题</td>
        <td>分类</td>
        <td>路径</td>
        <td>日期</td>
        <td>排序</td>
    </tr>
  </thead>
  <tbody>
      <?php
        $i = ($page_num - 1) * 5;
        for(; $last_page > 0 && $i < $page_count && $i < $page_num * 5 + 1; $i++ ){ 
            $page_key = $page_keys[$i];
            $page = $kw_pages[$page_key];
      ?>
        <tr<?php if ($i % 2 == 0) echo ' class="alt"'; ?>>
            <td><?php echo $i + 1; ?></td>
            <td>
                <a class="row_name" href="pageEdit.php?file=<?php echo $page_key; ?>"><?php echo htmlspecialchars($page['title']);?></a>
                <div class="row_tool">
                    <a class="link_button" href="pageEdit.php?file=<?php echo $page_key; ?>">编辑</a>
                    <?php if ($state == 'delete') { ?>
                    <a class="link_button" href="?revert=<?php echo $page_key; ?>&state=<?php echo $state; ?>">还原</a>
                    <a class="link_button" href="?delete=<?php echo $page_key; ?>&state=<?php echo $state; ?>">删除</a>
                    <?php } else { ?>
                    <a class="link_button" href="?delete=<?php echo $page_key; ?>&state=<?php echo $state; ?>">回收</a>
                    <?php } ?>
                    <a class="link_button" href="../?<?php echo $page_key; ?>/" target="_blank">查看</a>
                </div>
            </td>
            <td>
                <?php echo $kw_categories[$page['class']]['name'];?>
                <div class="row_tool">
                    <?php
                    if(intval($kw_categories[$page['class']]['class']) == 0){
                      echo '(单页)';
                    } else if(intval($kw_categories[$page['class']]['class']) == 1){
                      echo '(文本列表)';
                    } else if(intval($kw_categories[$page['class']]['class']) == 2){
                      echo '(图片列表)';
                    }
                    ?>
                </div>
            </td>
            <td><?php echo htmlspecialchars($page_key);?></td>
            <td><?php echo htmlspecialchars($page['date']);?></td>
            <td><?php echo htmlspecialchars($page['order']);?></td>
        </tr>
      <?php
        }
      ?>
  </tbody>
  <tfoot>
    <tr>
        <td>NO.</td>
        <td>标题</td>
        <td>分类</td>
        <td>路径</td>
        <td>日期</td>
        <td>排序</td>
    </tr>
  </tfoot>
</table>
</div>
<div class="table_list_tool">
   <span>
  </span>
  <span class="pager">
    共 <?php echo $page_count; ?> 项&nbsp;&nbsp;
    <a class="link_button" href="?state=<?php echo $state; ?>">&laquo;</a>
    <a class="link_button" href="?state=<?php echo $state; ?>&page=<?php echo $prev_page; ?>">&lsaquo;</a>
    第 <input type="text" value="<?php echo $page_num; ?>" id="page_input_2"/> / <?php echo $last_page; ?> 页
    <a class="link_button" href="?state=<?php echo $state; ?>&page=<?php echo $next_page; ?>">&rsaquo;</a>
    <a class="link_button" href="?state=<?php echo $state; ?>&page=<?php echo $last_page; ?>">&raquo;</a>
  </span>
</div>

<?php require 'foot.php' ?>